package com.car.loan.project.xr.util.repayment;

import io.swagger.annotations.ApiModelProperty;
import java.math.BigDecimal;
import java.math.MathContext;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huangding
 * @description
 * @date 2019/12/31 9:17
 */
public interface RepaymentFormulaInterface {

    /**
     * 获取还款计划
     *
     * @param repaymentPlanParam 还款参数
     * @return 还款公式
     */
    default List<RepaymentPlan> getRepaymentPlans(RepaymentPlanParam repaymentPlanParam) {
        Integer term = repaymentPlanParam.getTerm();
        List<RepaymentPlan> repaymentPlans = new ArrayList<>(term);
        LocalDate parse = LocalDate.parse(repaymentPlanParam.getFirstStatementDate());
        Integer gracePeriod = repaymentPlanParam.getGracePeriod();
        BigDecimal financingAmount = repaymentPlanParam.getFinancingAmount();
        for (int i = 0; i < term; i++) {
            RepaymentPlan repaymentPlan = new RepaymentPlan();
            repaymentPlan.setStatementDay(parse.plusMonths(i).toString());
            repaymentPlan.setRepaymentDay(parse.plusMonths(i).plusDays(gracePeriod).toString());

            if (i == 0) {
                repaymentPlan.setMonthRepayAmount(repaymentPlanParam.getFirstRepayInterest()
                    .add(repaymentPlanParam.getFirstRepayPrincipal()));
                repaymentPlan.setMonthRepayInterest(repaymentPlanParam.getFirstRepayInterest());
                repaymentPlan.setMonthRepayPrincipal(repaymentPlanParam.getFirstRepayPrincipal());
                financingAmount = financingAmount
                    .subtract(repaymentPlanParam.getFirstRepayPrincipal(), MathContext.DECIMAL128);
                repaymentPlan
                    .setPrincipalBalance(financingAmount.setScale(2, BigDecimal.ROUND_HALF_UP));

            } else if (i == (term - 1)) {
                repaymentPlan.setMonthRepayAmount(repaymentPlanParam.getLastRepayInterest()
                    .add(repaymentPlanParam.getLastRepayPrincipal()));
                repaymentPlan.setMonthRepayInterest(repaymentPlanParam.getLastRepayInterest());
                repaymentPlan.setMonthRepayPrincipal(repaymentPlanParam.getLastRepayPrincipal());
                financingAmount = financingAmount
                    .subtract(repaymentPlanParam.getLastRepayPrincipal(), MathContext.DECIMAL128);
                repaymentPlan
                    .setPrincipalBalance(financingAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
            } else {
                financingAmount = financingAmount
                    .subtract(repaymentPlanParam.getMonthRepayPrincipal(), MathContext.DECIMAL128);
                repaymentPlan
                    .setPrincipalBalance(financingAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
                repaymentPlan.setMonthRepayAmount(repaymentPlanParam.getMonthRepayInterest()
                    .add(repaymentPlanParam.getMonthRepayPrincipal()));
                repaymentPlan.setMonthRepayInterest(repaymentPlanParam.getMonthRepayInterest());
                repaymentPlan.setMonthRepayPrincipal(repaymentPlanParam.getMonthRepayPrincipal());
            }
            repaymentPlan.setTerm(i + 1);
            repaymentPlans.add(repaymentPlan);
        }
        return repaymentPlans;
    }

    /**
     * 获取还款信息
     *
     * @param repaymentInfoParam 还款信息参数
     * @return 还款信息 月还款额 首期还款额 首期账单日
     */
    default RepaymentInfo getRepaymentInfo(RepaymentInfoParam repaymentInfoParam) {
        throw new UnsupportedOperationException("未实现");
    }

    /**
     * 获取融资金额
     */
    default Integer getFinancingAmount(BigDecimal installmentAmount, BigDecimal rate,
        BigDecimal bankRate) {
        throw new UnsupportedOperationException("未实现");
    }

    /**
     * 获取最低开票金额
     * @param financingAmount
     * @param installmentAmount
     * @param carType
     * @return
     */
    default Integer getMiniBillingAmount(Integer financingAmount, BigDecimal installmentAmount,
        Integer carType) {
        throw new UnsupportedOperationException("未实现");
    }
}
